﻿using System;
using System.Linq;
using Landpy.CruiseControlNet.FacadeInterface.ConfigurationModule;
using Landpy.CruiseControlNet.FacadeInterface.RepositoryModule;
using Landpy.CruiseControlNet.Infrastructure.Aop.WrapAttribute;

namespace Landpy.CruiseControlNet.DomainLayer.RepositoryModule
{
    public class PermissionRepository : IPermissionRepository
    {
        private IProjectRepository ProjectRepository { get; set; }

        public PermissionRepository()
        {
            this.ProjectRepository = new ProjectRepository();
        }

        [NotThrowException]
        [ExceptionMessageBox]
        [LogException]
        public RolePermission GetInternalRolePermissionById(InternalSecurity internalSecurity, Guid id)
        {
            return (from RolePermission rolePermission in
                        (from Permission permission in internalSecurity.Permissions
                         where permission.Type == PermissionType.RolePermission
                         select permission)
                    where rolePermission.Id == id
                    select rolePermission).FirstOrDefault();
        }

        [NotThrowException]
        [ExceptionMessageBox]
        [LogException]
        public UserPermission GetInternalUserPermissionById(InternalSecurity internalSecurity, Guid id)
        {
            return (from UserPermission userPermission in
                        (from Permission permission in internalSecurity.Permissions
                         where permission.Type == PermissionType.UserPermission
                         select permission)
                    where userPermission.Id == id
                    select userPermission).FirstOrDefault();
        }

        [NotThrowException]
        [ExceptionMessageBox]
        [LogException]
        public RolePermission GetProjectRolePermissionById(Project project, Guid id)
        {
            return (from RolePermission rolePermission in
                        (from Permission permission in project.Security.Permissions
                         where permission.Type == PermissionType.RolePermission
                         select permission)
                    where rolePermission.Id == id
                    select rolePermission).FirstOrDefault();
        }

        [NotThrowException]
        [ExceptionMessageBox]
        [LogException]
        public UserPermission GetProjectUserPermissionById(Project project, Guid id)
        {
            return (from UserPermission userPermission in
                        (from Permission permission in project.Security.Permissions
                         where permission.Type == PermissionType.UserPermission
                         select permission)
                    where userPermission.Id == id
                    select userPermission).FirstOrDefault();
        }

        [NotThrowException]
        [ExceptionMessageBox]
        [LogException]
        public RolePermission GetProjectRolePermissionById(CruiseControl cruiseControl, Guid projectId, Guid id)
        {
            Project queryProject = this.ProjectRepository.GetProjectById(cruiseControl, projectId);
            return this.GetProjectRolePermissionById(queryProject, id);
        }

        [NotThrowException]
        [ExceptionMessageBox]
        [LogException]
        public UserPermission GetProjectUserPermissionById(CruiseControl cruiseControl, Guid projectId, Guid id)
        {
            Project queryProject = this.ProjectRepository.GetProjectById(cruiseControl, projectId);
            return this.GetProjectUserPermissionById(queryProject, id);
        }
    }
}
